package com.tokenmaster.aitrade.init.history_2014;

import java.util.ArrayList;
import java.util.List;

import com.tokenmaster.aitrade.common.system.KLinePoint;
import com.tokenmaster.aitrade.common.ticker.TickerData;
import com.tokenmaster.aitrade.data.SystemDB;

/**
 * 
 * @Description: K线集合
 * @author buxinyun
 * @date 2018年6月26日
 *
 */
public class KLinePoints_Set {

	private int timeInterval = 0;
	private int maxNum = 20;
	private String symbol = "";
	private String kType = "";

	/**
	 * K线集合
	 */
	private List<KLinePoint> kLinePoints = new ArrayList<KLinePoint>();

	/**
	 * 时间间隔
	 * 
	 * @param timeInterval
	 */
	public KLinePoints_Set(String symbol, String kType, int maxNum) {

		this.symbol = symbol;
		this.kType = kType;

		if (kType.equals("5min")) {
			timeInterval = 5 * 60 * 1000;
		} else if (kType.equals("15min")) {
			timeInterval = 15 * 60 * 1000;
		} else if (kType.equals("1hour")) {
			timeInterval = 60 * 60 * 1000;
		} else if (kType.equals("4hour")) {
			timeInterval = 4 * 60 * 60 * 1000;
		} else if (kType.equals("1day")) {
			timeInterval = 24 * 60 * 60 * 1000;
		}

		this.maxNum = maxNum;
	}

	/**
	 * 新的TickerData
	 * 
	 * @param tickerData
	 */
	public void newTicker(TickerData tickerData) {

		createKLine(tickerData, timeInterval);

		if (kLinePoints.size() > maxNum) {
			kLinePoints.remove(0);
		}

	}

	private void createKLine(TickerData tickerData, int timeInterval) {

		KLinePoint lastKLinePoint = getLastKLinePoint();

		if (lastKLinePoint == null) {

			// 计算第一根K线
			lastKLinePoint = new KLinePoint();
			kLinePoints.add(lastKLinePoint);

			lastKLinePoint.setSymbol(symbol);
			lastKLinePoint.setType(kType);
			lastKLinePoint.setUnixTime((long) (Math.floor(tickerData.getUnixtime() / timeInterval) * timeInterval));
			lastKLinePoint.setHigh(tickerData.getLast());
			lastKLinePoint.setLast(tickerData.getLast());
			lastKLinePoint.setLow(tickerData.getLast());
			lastKLinePoint.setOpen(tickerData.getLast());
		} else if (tickerData.getUnixtime() - lastKLinePoint.getUnixTime() > timeInterval) {

			// 保存老的K线
			SystemDB.getInstance().insertKLine(lastKLinePoint);

			// 超出时间间隔,重新生成K线
			lastKLinePoint = new KLinePoint();
			kLinePoints.add(lastKLinePoint);

			lastKLinePoint.setSymbol(symbol);
			lastKLinePoint.setType(kType);
			lastKLinePoint.setUnixTime((long) (Math.floor(tickerData.getUnixtime() / timeInterval) * timeInterval));
			lastKLinePoint.setHigh(tickerData.getLast());
			lastKLinePoint.setLast(tickerData.getLast());
			lastKLinePoint.setLow(tickerData.getLast());
			lastKLinePoint.setOpen(tickerData.getLast());

		} else {
			lastKLinePoint.setLast(tickerData.getLast());

			if (tickerData.getLast() > lastKLinePoint.getHigh())
				lastKLinePoint.setHigh(tickerData.getLast());
			if (tickerData.getLast() < lastKLinePoint.getLow())
				lastKLinePoint.setLow(tickerData.getLast());
		}

	}

	/**
	 * 返回最后一个K线点
	 * 
	 * @return
	 */
	public KLinePoint getLastKLinePoint() {
		if (kLinePoints.size() <= 0) {
			return null;
		}

		return kLinePoints.get(kLinePoints.size() - 1);
	}

	public List<KLinePoint> getkLinePoints() {
		return kLinePoints;
	}
}
